<?php
ob_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
 <script src="http://code.jquery.com/jquery-1.4.2.min.js" type="text/javascript"></script>
        <script>
	       
				function strikeWord(val)
		          {
                 if($("#testcheckbox_"+val).attr("checked")){ 
                    $("#testtextfield_"+val).css({"text-decoration":"line-through"});
				      }
				       else
				     $("#testtextfield_"+val).css({"text-decoration":"none"});
		          }
			
        </script>
</head>
<body>
<?php
include("connection/connection.php");
$frmId=$_POST['frmId'];
$populationFactor=$_POST['populationFactor'];
$village_id=$_POST['testcheckbox'];
$census=array_keys($village_id);
$DelVillage=implode(",",$village_id);
$query="select village_id,cvc from tbl_dtls_q1_main where id='$frmId'";
$result=mysql_query($query);
$row=mysql_fetch_array($result);
$vills=$row['village_id'];
$cvc=$row['cvc'];
$vills_arr=explode(",",$vills);
$cvc_arr=explode(",",$cvc);
$arr = array_diff($vills_arr, $village_id);
$arr_cvc = array_diff($cvc_arr, $census);
$village=implode(",",$arr);
$cvcs=implode(",",$arr_cvc);

$queryPop="select distinct habitation_code,total_population from tbl_lookup_location location,tbl_dtls_q1_oh OH where location.habitation_code=OH.habit_code and OH.status_covered=1 and location.village_id in($village)";
$resultPop=mysql_query($queryPop);
while($population=mysql_fetch_array($resultPop))
{
//echo $population['habitation_code'];
//echo"<br>";
$Pophabits[]=$population['total_population'];
}
//print_r($Pophabits);
//exit;
$population_benifited=array_sum($Pophabits);
$current_population=$population_benifited*$populationFactor;
$updateVillage="update tbl_dtls_q1_main set village_id='$village',population_benefited='$population_benifited',present_population='$current_population' where id='$frmId'";
$upvillresult=mysql_query($updateVillage);
$updatecvcs="update tbl_dtls_q1_main set cvc='$cvcs' where id='$frmId'";
$upcvcsresult=mysql_query($updatecvcs);
if($upvillresult)
{
$habit="select habitation_code from tbl_lookup_location where village_id in($DelVillage)";
$habitresult=mysql_query($habit);
while($rowhabit=mysql_fetch_array($habitresult))
{
$rowhabits=$rowhabit['habitation_code'];
$delVillage="delete from tbl_dtls_q1_oh where habit_code='$rowhabits' and loc_id='$frmId'";
$delvillresult=mysql_query($delVillage);

$del_crit_Village="delete from tbl_dtls_q1_critical where critical_hcode='$rowhabits' and loc_id='$frmId'";
$del_crit_villresult=mysql_query($del_crit_Village);

$del_crit_Village="delete from tbl_dtls_q1_tanker where habitation_code='$rowhabits' and loc_id='$frmId'";
$del_crit_villresult=mysql_query($del_crit_Village);

}
/* ---for district/block/panchayat update---- */
$mix_record="select distinct district_id,block_id,panchayat_id from tbl_lookup_location where village_id in($village)";
$mix_result=mysql_query($mix_record);
while($mix=mysql_fetch_array($mix_result))
{
$rest_district[]=$mix['district_id'];
$rest_block[]=$mix['block_id'];
$rest_panchayat[]=$mix['panchayat_id'];
}
$rest_district=array_unique($rest_district);
$rest_block=array_unique($rest_block);
$rest_panchayat=array_unique($rest_panchayat);
$dist_all=implode(",",$rest_district);
$block_all=implode(",",$rest_block);
$panchayat_all=implode(",",$rest_panchayat);
$updateUpper="update tbl_dtls_q1_main set district_id='$dist_all',block_id='$block_all',panchayat_id='$panchayat_all' where id='$frmId'";
$Upperresult=mysql_query($updateUpper);
/* ---end district/block/panchayat update---- */
header("location:q1_rwss_edit.php");
}
?>
</body>
</html>
